import { Injectable, ConflictException, Logger } from '@nestjs/common';
import { PrismaService } from '../common/services/prisma.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';

@Injectable()
export class UserService {
  constructor(private readonly prisma: PrismaService) {}

  private readonly users = [
    {
      userId: 1,
      username: 'john',
      password: 'changeme',
    },
    {
      userId: 2,
      username: 'maria',
      password: 'guess',
    },
  ];

  create(createUserDto: CreateUserDto) {
    return 'This action adds a new user';
  }

  findAll() {
    return `This action returns all user`;
  }

  async findOne(id: string) {
    const res = await this.prisma.user.findUnique({
      where: { id },
    });
    return res;
  }

  async update(id: string, updateUserDto: UpdateUserDto) {
    const res = await this.prisma.user.update({
      where: { id },
      data: {
        ...updateUserDto,
      },
    });
    return res;
  }

  async remove(id: string) {
    try {
      const res = await this.prisma.user.delete({
        where: { id },
      });
      return res;
    } catch (err) {
      Logger.error(JSON.stringify(err));
      throw new ConflictException();
    }
  }
}
